package member

import (
	"gpt-api/framework/ginplus"
	"gpt-api/pkg/model"
	"gpt-api/pkg/response"

	"github.com/gin-gonic/gin"
)

// 指定父级ID下的子关系列表
func (h *Handler) Children(c *gin.Context) error {
	// 获取参数
	var req struct {
		Parent int `form:"parent"`
	}

	// 查询子级关系
	relations, err := (&model.Relations{}).GetChildren(h.db, req.Parent)
	if err != nil {
		return err
	}

	// 遍历数据
	relat := make([]*response.RelationResp, 0)
	for _, relations := range relations {
		relat = append(relat, &response.RelationResp{
			ID:      uint64(relations.ID),
			Name:    relations.Name,
			Parent:  uint64(relations.Parent),
			OnlyOne: uint64(relations.OnlyOne),
		})
	}
	return ginplus.ReplyJSON(c, relat)
}

// 获取成员的关系列表
func (h *Handler) Relations(c *gin.Context) error {
	// 验证参数
	userId := ginplus.GetUserID(c)
	var req struct {
		MemberId uint64 `form:"member_id" binding:"required,gte=0"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 获取唯一关系集合
	only_relations, err := (&model.Relations{}).GetRelationsForOnly(h.db)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	// 唯一关系ID
	only_relations_ids := make([]uint64, len(only_relations))
	for i, v := range only_relations {
		only_relations_ids[i] = uint64(v.ID)
	}

	// 查询成员的已有唯一关系
	var have_relations_ids []uint64
	member_relations, err := (&model.Member{}).GetMemberForRelationId(h.db, userId, []uint64{req.MemberId}, only_relations_ids)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	for _, member_relations := range member_relations {
		have_relations_ids = append(have_relations_ids, uint64(member_relations.RelationID))
	}

	// 递归查询成员所有兄弟姐妹
	allBrothersIds, err := (&model.Member{}).GetMemberBrothersIdsForDown(h.db, userId, []uint64{req.MemberId})
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	allBrothersIds = append(allBrothersIds, req.MemberId)

	// 查询所有兄弟姐妹已有的父母关系是否存在,并获取父母ID集合
	var parents_ids []uint64
	parents_relations, err := (&model.Member{}).GetMemberForRelationId(h.db, userId, allBrothersIds, []uint64{uint64(model.RelationsFather), uint64(model.RelationsMother)})
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}
	for _, parents_relations := range parents_relations {
		have_relations_ids = append(have_relations_ids, uint64(parents_relations.RelationID))
		parents_ids = append(parents_ids, uint64(parents_relations.ID))
	}

	// 判断是否存在父母
	if len(parents_ids) > 0 {
		// 查询伴侣关系是否存在
		partrer_relations, err := (&model.Member{}).GetMemberForRelationId(h.db, userId, parents_ids, []uint64{uint64(model.RelationsPartner)})
		if err != nil {
			return ginplus.ReplyInternalError(c, err)
		}
		if len(partrer_relations) > 0 {
			// 查询伴侣是否是别人儿子或女儿,如果是判断性别1=父亲，2=母亲
			have_relations_ids = append(have_relations_ids, uint64(model.RelationsFather), uint64(model.RelationsMother))
		}
	}

	// 查询成员是否是别人儿子或女儿,如果是判断性别1=父亲，2=母亲
	// son_daughter_relations, err := (&model.Member{}).GetMemberForRelationId(h.db, userId, []uint64{req.MemberId}, []uint64{uint64(model.RelationsDaughter), uint64(model.RelationsSon)})
	// if err != nil {
	// 	return ginplus.ReplyInternalError(c, err)
	// }
	// for _, son_daughter_relations := range son_daughter_relations {
	// 	// 判断性别男女
	// 	if son_daughter_relations.Gender == int(model.MemberGenderMale) { // 父亲
	// 		have_relations_ids = append(have_relations_ids, uint64(model.RelationsFather))
	// 	}
	// 	if son_daughter_relations.Gender == int(model.MemberGenderFemale) { // 母亲
	// 		have_relations_ids = append(have_relations_ids, uint64(model.RelationsMother))
	// 	}
	// }

	// 查询成员可添加关系
	relations, err := (&model.Relations{}).GetRelationsNotId(h.db, have_relations_ids)
	if err != nil {
		return ginplus.ReplyInternalError(c, err)
	}

	// 遍历数据
	relat := make([]*response.RelationResp, 0)
	for _, relations := range relations {
		relat = append(relat, &response.RelationResp{
			ID:      uint64(relations.ID),
			Name:    relations.Name,
			Parent:  uint64(relations.Parent),
			OnlyOne: uint64(relations.OnlyOne),
		})
	}
	return ginplus.ReplyJSON(c, relat)
}
